package net.esj.basic.core.business.provider;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import net.esj.basic.core.engine.factory.bean.BeanFactory;
import net.esj.basic.pojo.MObject;

@Singleton
@Named("melon.MObjectProvidersFactory")
public class MObjectProvidersFactory{

	private static Map<String,MObjectProvider> providers = new HashMap<String,MObjectProvider>();

	private BeanFactory beanFactory;
	
	 public static MObject get(Serializable id,String type){
		MObjectProvider provider = providers.get(type);
		if(provider!=null){
			return provider.get(id, type);
		}
		return MObject.EMPTY;
	}
	
	 public static MObject get(MObject obj){
		return get(obj.getId(), obj.getMType()); 
	 }

	 @Inject
	public void setBeanFactory(BeanFactory beanFactory) {
		this.beanFactory = beanFactory;
		Map<String,MObjectProvider> pros = beanFactory.getBeansByType(MObjectProvider.class);
		for(MObjectProvider pro : pros.values()){
			if(pro!=null){
				providers.put(pro.getMType(), pro);
			}
		}
	}
	 
	 public static void regisityProvider(MObjectProvider provider){
		 providers.put(provider.getMType(), provider);
	 }
}
